<template>
  <el-dialog :title="title" :visible.sync="dialogShow">
    <el-form ref="form" :model="formData" :rules="formRules" label-width="220px" @close="closeDialog">
      <el-form-item label="权限名称" prop="name">
        <el-input v-model="formData.name" />
      </el-form-item>
      <el-form-item label="权限标识" prop="name">
        <el-input v-model="formData.code" />
      </el-form-item>
      <el-form-item label="权限描述" prop="name">
        <el-input v-model="formData.description" />
      </el-form-item>
      <el-form-item label="是否可用" prop="name">
        <el-switch v-model="formData.enVisible" active-value="1" inactive-value="0" />
      </el-form-item>
      <el-form-item label="">
        <el-button size="small" type="primary" @click="btnOk">确定</el-button>
        <el-button size="small" @click="dialogShow = 'false'">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { addPermissionApi, editPermissionApi, getPermissionInfoApi } from '@/api/permission'
export default {
  data() {
    return {
      dialogShow: false,
      formData: {
        name: '',
        code: '',
        description: '',
        type: '', // type的值为1和2，对应的是菜单权限和按钮权限
        pid: '',
        enVisible: '0'
      },
      formRules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    title() {
      return this.formData.id ? '修改' : '新增'
    }
  },
  methods: {
    async btnOk() {
      // 1.表单校验
      try {
        await this.$refs.form.validate()
      } catch (error) {
        return console.log(error)
      }
      // 修改
      if (this.formData.id) {
        await editPermissionApi(this.formData)
        this.$message.success('修改成功')
      } else {
        // 2.发生请求
        await addPermissionApi(this.formData)
        // 3.提示成功
        this.$message.success('添加成功')
      }
      // 4.列表更新
      this.$emit('updateList')
      // 5.弹框关闭
      this.dialogShow = false
    },
    closeDialog() {
      this.formData = {
        name: '',
        code: '',
        description: '',
        type: '', // type的值为1和2，对应的是菜单权限和按钮权限
        pid: '',
        enVisible: '0'
      }
      this.$refs.form.clearValidate()
    },
    // 回显逻辑
    async getRowInfo(Id) {
      const res = await getPermissionInfoApi(Id)
      //   console.log(res)
      this.formData = res
      this.dialogShow = true
    }
  }
}
</script>

<style>

</style>
